﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Mvc;
using Kelper.Domain.Data;
using Kelper.Domain.Data.Abstract;
using Kelper.Infrastructure.Abstract;
using Ninject;

namespace Kelper.Infrastructure
{
    public class ControllerFactory : DefaultControllerFactory
    {
        private IKernel kernel;

        public ControllerFactory()
        {
            kernel = new StandardKernel();

            AddBindings();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController)kernel.Get(controllerType);
        }

        private void AddBindings()
        {
            KelperDbContext db = new KelperDbContext();
            kernel.Bind<KelperDbContext>().ToConstant(db);

#if DEBUG
            kernel.Bind<IRegistrationConfirmation>().To<FileConfirmation>();
#else
            kernel.Bind<IRegistrationConfirmation>().To<EmailConfirmation>();
#endif

            kernel.Bind<IUserRepository>().To<UserRepository>();
            kernel.Bind<ICategoryRepository>().To<CategoryRepository>();
            kernel.Bind<IRecipeRepository>().To<RecipeRepository>();
            kernel.Bind<IIngredientRepository>().To<IngredientRepository>();
        }
    }
}